//
//  LCSpirtualityViewController.m
//  JKFuYinProject
//
//  Created by jiukuyinyue on 2017/9/18.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCSpirtualityViewController.h"
#import "SpirtualityModel.h"
#import "LCSpirtualityTableViewCell.h"


#define SpirtualityCell @"SpirtualityCell"

@interface LCSpirtualityViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;//
@property (nonatomic, strong) NSArray<SpirtualityModel *> *lingxiuArr;


@end

@implementation LCSpirtualityViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"每日灵粮";
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.tableView registerClass:[LCSpirtualityTableViewCell class] forCellReuseIdentifier:SpirtualityCell];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //添加以下代码关闭估算行高
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self loadLingXiuData];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    [self setupTableViewLayout];
}
- (void)setupTableViewLayout
{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-0);
        }
    }];
}
- (void)setupTableViewCompatibilityADLayout
{
    CGFloat width = MIN(self.view.width , self.view.height);
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-width/20*3);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-width/20*3);
        }
    }];
}
#pragma mark - 查询数据
- (void)loadLingXiuData
{
    @weakify(self)
    [LSZCacheHandling initWithfindPlist:lingxiu :^(BOOL isOk, NSDictionary * dic) {
        @strongify(self)
        if (isOk == YES) {
            self.lingxiuArr = [NSArray yy_modelArrayWithClass:[SpirtualityModel class] json:dic[@"data"]].copy;
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
        }else{
            [self addDataSpirtuality];
        }
    }];
}
#pragma mark - 请求的数据
-(void)addDataSpirtuality
{
    @weakify(self)
    NSDictionary * dic = @{@"type":@"spirituality", @"parentID":@"0"};
    [HttpTool postWithURL:kBasePostFuyin params:dic success:^(id json) {
        @strongify(self)
        [LSZCacheHandling initAddPathPraisePoetry:lingxiu NSDictionary:json];
        self.lingxiuArr = [NSArray yy_modelArrayWithClass:[SpirtualityModel class] json:json[@"data"]].copy;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self.tableView reloadData];
        }];
    } failure:^(NSError *error) {
        NSLog(@"error = %@", error);
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.lingxiuArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCSpirtualityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SpirtualityCell forIndexPath:indexPath];
    cell.model = self.lingxiuArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height;
    height = 200;
    return height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZPlayImageAndTableViewVC * vc = [[LSZPlayImageAndTableViewVC alloc]init];
    SpirtualityModel * model = self.lingxiuArr[indexPath.row] ;
    vc.imageTopBack = model.image300;
    vc.strTopImageView = model.image;
    vc.titleStr = model.title ;
    vc.type = @"lingxiuSongs";
    vc.parentId = model.id;
    vc.typeComeWhere = @"2";
    vc.Update = model.des;//更新多少了
    vc.date3Array = model.date3;
    vc.lastPageNumberRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
